/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mvc;

import com.google.common.net.InetAddresses;
import java.io.IOException;
import java.io.Serializable;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.channels.IllegalBlockingModeException;
import javax.ejb.Stateless;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author tadiwa
 */
@ManagedBean
@Stateless
@ViewScoped
public class ServerCheck implements Serializable {

    private String authip;
    private String pacip;
    private static final Logger log = LoggerFactory.getLogger(ShiroLoginBean.class);

    public ServerCheck() {
    }

    public void doCheck() {
        int default_timeout = 2000; // timeout used if timeout not provided

        //String host = "10.210.0.93";
        String port = "10389";

        boolean isValid = InetAddresses.isInetAddress(authip);
        if (isValid) {
            Socket socket = new Socket();
            InetSocketAddress endPoint = new InetSocketAddress(authip,
                    Integer.parseInt(port));

            if (endPoint.isUnresolved()) {

                System.out.println(" Not able to resolve host " + endPoint);

            } else {
                try {
                    System.out
                            .println("------------------------------------------------->>>PAC SERVER CONN START");
                    System.out.println("Testing Connection -> IP: " + authip + " PORT: " + port);
                    socket.connect(endPoint, default_timeout);
                    System.out.printf(
                            "Connected:    %s  \n " + endPoint.getHostName() + "",
                            endPoint);
                    System.out
                            .println("\n------------------------------------------------->>>PAC SERVER CONN END");
                    facesError("Connected to: " + endPoint.getHostName());
                } catch (IOException ioe) {

                    System.out.printf(
                            "Failed to connect:    %s message: %s - %s \n",
                            endPoint, ioe.getClass().getSimpleName(),
                            ioe.getMessage());
                    facesError(ioe.getMessage());
                } catch (IllegalBlockingModeException ibme) {
                    System.out.printf(
                            "Failed to connect:    %s message: %s - %s \n",
                            endPoint, ibme.getClass().getSimpleName(),
                            ibme.getMessage());
                    facesError(ibme.getMessage());
                } catch (IllegalArgumentException iae) {

                    System.out.printf(
                            "Failed to connect:    %s message: %s - %s \n",
                            endPoint, iae.getClass().getSimpleName(),
                            iae.getMessage());
                    facesError(iae.getMessage());
                } finally {
                    if (socket != null) {
                        try {
                            socket.close();
                        } catch (IOException ioex) {
                            System.out.printf(ioex.getMessage());
                        }
                    }

                }
            }
        }else {
            facesError("Invalid IP address");
        }

    }

    public String getAuthip() {
        return authip;
    }

    public void setAuthip(String authip) {
        this.authip = authip;
    }

    public String getPacip() {
        return pacip;
    }

    public void setPacip(String pacip) {
        this.pacip = pacip;
    }

    private void facesError(String message) {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, message, null));
    }

}
